package main

import (
	"log"
	"os"

	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
)

func main() {
	const appID = "com.github.gotk3.gotk3-examples"
	application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
	if err != nil {
		log.Fatal("Could not create application:", err)
	}

	application.Connect("activate", func() {
		win := newWindow(application)

		//新建窗口事件"app.new"
		appNew := glib.SimpleActionNew("new", nil)
		appNew.Connect("activate", func() {
			log.Printf("app.new")
			newWindow(application).ShowAll()
		})
		application.AddAction(appNew)

		//推出事件“app.quit”
		appQuit := glib.SimpleActionNew("quit", nil)
		appQuit.Connect("activate", func() {
			log.Printf("app.quit")
			application.Quit()
		})
		application.AddAction(appQuit)

		win.ShowAll()

	})

	os.Exit(application.Run(os.Args))
}

func newWindow(application *gtk.Application) *gtk.ApplicationWindow {
	win, err := gtk.ApplicationWindowNew(application)
	if err != nil {
		log.Fatal("Unable to create window:", err)
	}

	win.SetTitle("GOTK3 Actions Example")

	// Label text in the window
	bodyLabel, err := gtk.LabelNew("Use the menuList button to test the actions")
	if err != nil {
		log.Fatal("Could not create label:", err)
	}

	setAction(win, bodyLabel)

	// Create a new menuList button
	//创建一个下拉菜单按钮
	menuBtn := createMenuButton()
	// add the menuList button to the header
	// Create a header bar
	header := createHeadBar()
	// 在header左边的添加下拉菜单按钮
	header.PackStart(menuBtn)

	// Assemble the window
	win.Add(bodyLabel)
	win.SetTitlebar(header)
	win.SetPosition(gtk.WIN_POS_MOUSE)
	win.SetDefaultSize(600, 300)
	return win
}

func setAction(win *gtk.ApplicationWindow, bodyLabel *gtk.Label) {
	// Create the action "win.close"
	//创建事件"win.close",默认是事件组为win
	aClose := glib.SimpleActionNew("close", nil)
	aClose.Connect("activate", func() {
		log.Printf("win.close")
		win.Close()
	})
	win.AddAction(aClose)

	// Create and insert custom action group with prefix "custom"
	//创建事件：其前缀为“custom”
	customActionGroup := glib.SimpleActionGroupNew()
	win.InsertActionGroup("custom", customActionGroup)

	// Create an action in the custom action group
	aPanic := glib.SimpleActionNew("panic", nil)
	aPanic.Connect("activate", func() {
		bodyLabel.SetLabel("PANIC!")
	})
	//将“panic”事件加入组"custom"
	customActionGroup.AddAction(aPanic)
	//这里直接将“panic”加入win，则在触发事件“win.panic”时也会执行activate
	win.AddAction(aPanic)
}

func createMenuButton() *gtk.MenuButton {
	//定义创建下拉菜单方法
	var createMenuList = func() *glib.Menu {
		// Set up the menu model for the button
		menu := glib.MenuNew()
		if menu == nil {
			log.Fatal("Could not create menu (nil)")
		}
		// Actions with the prefix 'app' reference actions on the application
		// Actions with the prefix 'win' reference actions on the current window (specific to ApplicationWindow)
		// Other prefixes can be added to widgets via InsertActionGroup
		menu.Append("New Window", "app.new")
		menu.Append("Close Window", "win.close")
		menu.Append("Custom Panic", "custom.panic")
		menu.Append("Panic", "win.panic")
		menu.Append("Quit", "app.quit")
		return menu
	}

	//创建一个菜单按钮
	menuBtn, err := gtk.MenuButtonNew()
	if err != nil {
		log.Fatal("Could not create createMenuList button:", err)
	}

	menuList := createMenuList()
	//设置下拉菜单按钮列表
	menuBtn.SetMenuModel(&menuList.MenuModel)
	return menuBtn
}

func createHeadBar() *gtk.HeaderBar {
	header, err := gtk.HeaderBarNew()
	if err != nil {
		log.Fatal("Could not create header bar:", err)
	}
	header.SetShowCloseButton(true)
	header.SetTitle("GOTK3")
	header.SetSubtitle("Actions Example")
	return header
}
